<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            故障提报编辑
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">设备编码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描设备编码" id="equipCode" value="" onclick="getIdSelector();" disabled />
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机大类</div>
                    <div class="aui-list-item-input">
                        <select onchange="ext03Change()" id="ext03" disabled>
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机原因</div>
                    <div class="aui-list-item-input">
                        <select onchange="ext05List()" id="ext04" disabled>
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">停机内容</div>
                    <div class="aui-list-item-input" >
                        <select id="ext05" disabled>
                        </select>
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label ui-selectee">故障描述</div>
                    <div class="aui-list-item-input ui-selectee"> <input class="form-control" type="text"
                            id="description">
                    </div>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">故障提报时间</div>
                    <div class="aui-list-item-input">
                        <input readonly class="form-control" type="text" id="faultdate" placeholder="请选择">
                    </div>
                </div>
            </li> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">维修开始时间</div>
                    <div class="aui-list-item-input">
                        <input readonly class="form-control" type="text" id="beginDate" placeholder="请选择">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">维修结束时间</div>
                    <div class="aui-list-item-input">
                        <input readonly class="form-control" type="text" id="endDate" placeholder="请选择">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">实际工时(分)</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="actualtime">
                    </div>
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">班组</div>
                    <div class="aui-list-item-input">
                        <select id="teamGid">
                        </select>
                    </div>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">横向展开</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="horizontalExpansion">
                    </div>
                </div>
            </li> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">维修描述</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="repairDescription">
                    </div>
                </div>
            </li> -->
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">原因分析</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="causeAnalysis">
                    </div>
                </div>
            </li> -->
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">再发防止对策</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="recurrencePrevention">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="width: 45%;">备注</div>
                    <div class="aui-list-item-input">
                        <input class="form-control" type="text" id="remark">
                    </div>
                </div>
            </li>
            <!-- <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">状态</div>
                    <div class="aui-list-item-input">
                        <select id="status">
                    </select>
                    </div>
                </div>
            </li> -->

        </ul>
        <p></p>
        <p></p>
        <div class="aui-btn aui-btn-block" style="background-color:#285688;color: #ffffff" onclick="save()">保存</div>

    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/jdate.min.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var equipCode = ""; //设备编码
    var faultLevel = ""; //故障等级
    var faultClass = ""; //故障类别
    var description = ""; //故障描述
    var faultdate = ""; //故障提报时间
    //var status = ""; //状态
    var teamGid = ""; //班组
    var ss = [];
    //第一次进入页面的停机原因的sysid
    var mysysid = "";
    // var beginDate = ""; //维修开始时间
    // var endDate = ""; //维修结束时间
    // var actualtime = ""; //实际工时(分)
    //var horizontalExpansion = ""; //横向展开
    //var repairDescription = ""; //维修描述
    // var causeAnalysis = ""; //原因分析
    var recurrencePrevention = ""; //再发防止对策
    var remark = ""; //备注
    var readyNum = 0; //用来判断下拉框等组件是否初始化完成

    //初始化查询仓库下拉框
    apiready = function () {
        api.parseTapmode();
        $("#equipCode").focus();
        idSelector = "equipCode";

        //查询故障等级下拉框
        doSearchMdparaconfig("emEquipmentFaultLevel", "faultLevel");
        //查询故障类别下拉框
        doSearchMdparaconfig("emEquipmentFaultClass", "faultClass");
        //查询状态下拉框
        doSearchMdparaconfig("emEquipmentFaultStatus", "status");
        //查询班组下拉框
        doSearchTeamGid();
        //获取信息
        getEmequipmentInfo(api.pageParam.sysid)


        new Jdate({
            el: '#faultdate',
            format: 'YYYY-MM-DD hh:mm:ss',
            beginYear: 2000,
            endYear: 2100
        });


    }
    //获取信息
    function getEmequipmentInfo(sysid) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getEmequipmentfaultDataInfo";
        var data = {
            values: {
                sysid: sysid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, (ret, err) => { // 使用箭头函数绑定 this
            if (ret) {
                var data = JSON.stringify(ret);
                console.log("@@@@@" + data)
                var myobj = eval("(" + data + ")");
                //返回成功
                if (myobj.code == "0") {
                    var entity = eval(myobj.entity);
                    $("#equipCode").val(entity.equipCode);
                    $("#description").val(entity.description);
                    $("#recurrencePrevention").val(entity.recurrencePrevention);
                    $("#remark").val(entity.remark);
                    $("#teamGid").find("option:contains('" + entity.teamId + "')").attr("selected", true);
                    mysysid = entity.ext06
                    this.doSearchExt03(entity.ext03)
                    this.typeExt03Change(entity.ext03, entity.ext04)
                    this.typeClassChange(entity.ext06, entity.ext05);
                    //失败
                } else if (myobj.code == "1") {
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：设备编码
            if (idSelector == 'equipCode') {

                $("#equipCode").val(ret);
                //查询仓库
                //getWorkCenter(ret, idSelector);
            }
        });
    }

    //业务参数下拉框
    function doSearchMdparaconfig(paracls, idH) {
         var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMdparaconfig";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                paracls: paracls,
                 un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "<option value=''>--请选择--</option>";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                }
                $api.html($api.byId(idH), html);
            }
        });
    }

    //班组下拉框
    function doSearchTeamGid() {
         var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                tablename: "md_labour_group",
                column_name: "group_name",
                column_value: "sysid",
                condition: "is_del = 0",
                 un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, function (ret, err) {
            readyNum = readyNum + 1;
            if (ret) {
                var data = JSON.stringify(ret);
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("teamGid"), html);
            }
        });
    }
    function typeClassChange(sysid, ext05) {
        console.log("sysid" + sysid)
        console.log(ext05)
         var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_detail',
                column_name: 'name',
                column_value: 'code',
                condition: 'is_del = 0 and md_shutdown_type_id = ' + "'" + sysid + "'",
                 un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                var map = ret;
                var list = map.list || [];
                var data = JSON.stringify(list);
                console.log(data)
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("ext05"), html);
                $("#ext05").val(ext05);
            } else {
                console.error("API error: ", err);
            }
        });
    }

    function ext05List() {
        // 获取停机原因的value
        var value = $("#ext04").val();
         var un = localStorage.getItem('un');
        console.log("！！！！！！", JSON.stringify(value))
        var shutdownid = ''
        console.log("@@@@@@@", JSON.stringify(ss))
        for (var i = 0; i < ss.length; i++) {
            console.log("Label:", ss[i].label);
            console.log("Value:", ss[i].value);
            if (value == ss[i].value) {
                shutdownid = ss[i].sysid;
                console.log("shutdownid:", shutdownid);
            }
        }
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_detail',
                column_name: 'name',
                column_value: 'code',
                condition: 'is_del = 0 and md_shutdown_type_id = ' + "'" + shutdownid + "'",
                 un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                var map = ret;
                var list = map.list || [];
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("ext05"), html);
            } else {
                console.error("API error: ", err);
            }
        });
    }
    function typeExt03Change(ext03, ext04) {
        var serverUrl = localStorage.getItem('serverUrl');
         var un = localStorage.getItem('un');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_type',
                column_name: 'name',
                column_value: 'code',
                other_column: 'sysid',
                condition: 'is_del=0 and type = ' + "'" + ext03 + "'",
                 un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                readyNum = readyNum + 1;
                var data = JSON.stringify(ret);
                console.log(data)
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                console.log("mmmmmmmmmmmmmm" + JSON.stringify(list))
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("ext04"), html);
                ss = eval(map.list);
                $("#ext04").val(ext04);
            } else {
                console.error("API error: ", err);
            }
        });
    }
    function ext03Change() {
        // 获取停机大类的值
        var a = $("#ext03").val();
        console.log(a)
        var serverUrl = localStorage.getItem('serverUrl');
         var un = localStorage.getItem('un');
        var url = serverUrl + "/app/comboxList";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                tablename: 'md_shutdown_type',
                column_name: 'name',
                column_value: 'code',
                other_column: 'sysid',
                condition: 'is_del=0 and type = ' + "'" + a + "'",
                 un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, (ret, err) => { // 使用箭头函数确保 this 指向正确
            if (ret) {
                var data = JSON.stringify(ret);
                console.log(data)
                var map = eval("(" + data + ")");
                var list = eval(map.list);
                console.log("mmmmmmmmmmmmmm" + JSON.stringify(list))
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("ext04"), html);
                ss = eval(map.list);
                console.log("$$$$$$$$$$$" + JSON.stringify(ss))
                this.ext05List()
            } else {
                console.error("API error: ", err);
            }
        });
    }
    //停机大类下拉框
    function doSearchExt03(ext03) {
        var serverUrl = localStorage.getItem('serverUrl');
         var un = localStorage.getItem('un');
        var url = serverUrl + "/app/comboxListBycodeType";
        var data = {
            values: { // 以表单方式提交参数（JSON对象）
                code: 'SHUTDOWN_TYPE',
                removeDefault: 'Y',
                 un: un
            }
        };

        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data,
        }, (ret, err) => { // 使用箭头函数绑定 this
            if (ret) {
                readyNum = readyNum + 1;
                var map = ret;
                var list = map.list || [];
                var html = "";
                for (var i = 0; i < list.length; i++) {
                    if (list[i].value == "1") {
                        continue;
                    }
                    html += "<option value='" + list[i].value + "'>" + list[i].label + "</option>";
                }
                $api.html($api.byId("ext03"), html);
                $("#ext03").val(ext03);
            } else {
                console.error("API error: ", err);
            }
        });
    }

    //保存按钮
    function save() {
        //设备编码
        var equipCode = $("#equipCode").val();
        //停机大类
        var ext03 = $("#ext03").val();
        //停机原因
        var ext04 = $("#ext04").val();
        //停机内容
        var ext05 = $("#ext05").val();
        //故障描述
        var description = $("#description").val();
        //故障提报时间
        // var faultdate = $("#faultdate").val();
        //班组
        var teamGid = $("#teamGid").val();
        //箱再发防止对策
        var recurrencePrevention = $("#recurrencePrevention").val();
        //备注
        var remark = $("#remark").val();

        //设备非空校验
        if (equipCode == "" || equipCode == null || equipCode == undefined) {
            api.toast({
                msg: '请选择/扫描设备',
                duration: 3000
            });
            return;
        }

        //停机大类
        if (ext03 == "" || ext03 == null || ext03 == undefined) {
            api.toast({
                msg: '请选择停机大类',
                duration: 3000
            });
            return;
        }
        //停机原因
        if (ext04 == "" || ext04 == null || ext04 == undefined) {
            api.toast({
                msg: '请选择停机原因',
                duration: 3000
            });
            return;
        }
        //停机内容
        if (ext05 == "" || ext05 == null || ext05 == undefined) {
            api.toast({
                msg: '请选择停机内容',
                duration: 3000
            });
            return;
        }



        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un'); //登录用户
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/emequipmentfaultEdit";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                sysid: api.pageParam.sysid,
                equipCode: equipCode, //设备编码
                ext03: ext03, // 停机大类
                ext04: ext04, //停机原因
                ext05: ext05, //停机内容
                description: description, //故障描述
                // faultdate: faultdate, //故障提报时间
                teamGid: teamGid, //班组
                recurrencePrevention: recurrencePrevention, //再发防止对策
                remark: remark, //备注
                status: "0", //状态为 未完成
                ext01: "3", //app状态-1-待维修
                un: un //登录用户
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            async: false,
            data: data
        }, function (ret, err) {
            console.log(ret);
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    // api.openWin({
                    //     name: "emequipmentfaultSubmitList",
                    //     url: "emequipmentfaultSubmitList" + '.html',
                    // });
                    api.closeWin({});

                } else if (myobj.code == '1') {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }



    //扫描之后条码解析
    function tmSplit(sm) {
        var arr = sm.split(",");
        // if (arr.length == 10) {
        $("#tm").val(sm);
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
        }
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            $("#qty").val(arr[2]);
        }
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            $("#boxnum").val(arr[9]);
        }
        saveAuto();
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#boxnum").select();
            //调用条码解析+保存事件
            tmSplit(tm);
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>